<?php
include_once("ClsSebent/config.php");
//if($rem_img=="y" && $_SESSION['SITE_ID'] != "" && $pro_id !="")

if(!$pro_id)
{
	$q=" select * from `site_product` where `site_id`= '".$_SESSION['SITE_ID']."' ";
	$r=mysql_query($q);
	$site_product=mysql_fetch_array($r,MYSQL_ASSOC);
	
	$pro_id=$site_product['product_id'];
}

/*if( $_SESSION['SITE_ID'] && $pro_id )
{

		$sql="UPDATE product_master SET  product_img='' WHERE id=".$pro_id;
		//echo $sql;
		mysql_query($sql);
header("Location: page_products.php?site_id=".$_SESSION['SITE_ID']."&pro_id=".$pro_id);

	}*/
	include_once("king_head.php");
	

	if($_REQUEST['action']=="Add")
	{
	$sql="INSERT INTO product_master (product_title, product_category, product_desc, product_img, product_img_alt, product_price, size, color, lot_size, lot_discount, max_discount) VALUES ('$product_title', '$product_category', '$product_desc', '$product_img_name', '$product_img_alt', '$product_price', '$size', '$color', $lot_size, $lot_discount, $max_discount)";
	
		//echo $sql;
		mysql_query($sql);
		$pid=mysql_insert_id();
		if (isset($product_img) && $product_img != "" && $tmp_img=="") {

			if(!copy($product_img,"./photos/products/${pid}_" . $product_img_name)){ echo "coping error!"; exit;}
	
		}
		if($tmp_pic!="")
		{
		$tmp_img1=substr(strrchr($tmp_img,"_"),1,strlen(strrchr($tmp_img,"_"))); //echo $tmp_img1;
		if(!copy("./photos/Temp_products/".$tmp_img,"./photos/products/${pid}_" . $tmp_img1)){ echo "coping error!"; exit;}
		$pqry="Update product_master SET product_img='".$tmp_img1."' where id=".$pid;
		mysql_query($pqry);
		$pro_id=$pid;
		//echo $pqry;
		}		
		echo "<H4><FONT COLOR=red>Product Added Successfully</FONT>!</H4>";
	}
	
	if($_REQUEST['action']=="Save" && $_REQUEST['pro_id'])
	{
		$sql="UPDATE product_master SET product_title='$product_title', product_category='$product_category', product_desc='$product_desc', product_img_alt='$product_img_alt', product_price='$product_price', size='$size', color='$color', lot_size=$lot_size, lot_discount=$lot_discount, max_discount=$max_discount";
		if (isset($product_img) && $product_img != "") {

			if(!copy($product_img,"./photos/products/${pro_id}_" . $product_img_name)){ echo "coping error!"; exit;}
	
			$sql=$sql . ", product_img='$product_img_name'";
		}
		$sql=$sql . " WHERE id='$pro_id'";
		//echo $sql;
		mysql_query($sql);
		echo "<H4><FONT COLOR=red>Product Saved Successfully</FONT>!</H4>";
	}
	
	if($action=="Delete" && $pro_id)
	{
		$sql="DELETE FROM product_master WHERE id='$pro_id'";
		//echo $sql;
		mysql_query($sql);
		$pid=$pro_id;
		echo "<H4><FONT COLOR=red>Product Deleted Successfully</FONT>!</H4>";
	}

	
	if($tmp_id)
	$sql="SELECT * FROM temp_product WHERE id=".$tmp_id;
	else
	$sql="SELECT * FROM `product_master` WHERE id='".$pro_id."'";
//echo $sql;
	$result = mysql_query($sql);
	if($row=mysql_fetch_array($result))
	{
		$product_title=$row["product_title"];
		$product_category=$row["product_category"];
		$product_desc=$row["product_desc"];
		$product_img=$row["product_img"];
		$product_img_alt=$row["product_img_alt"];
		$product_price=$row["product_price"];
		$size=$row["size"];
		$color=$row["color"];
		$lot_size=$row["lot_size"];
		$lot_discount=$row["lot_discount"];
		$max_discount=$row["max_discount"];
	}
	else{
		$product_title="";
		$product_category="";
		$product_desc="";
		$product_img="";
		$product_img_alt="";
		$product_price="";
		$size="";
		$color="";
		$lot_size="";
		$lot_discount="";
		$max_discount="";
	}
?>

<CENTER>
<TABLE width="100%">
<TR width="100%">
	<TD nowrap align=right>
	<form action="page_products.php"><input type='hidde'n name='site_id' value="<?php echo $_SESSION['SITE_ID']; ?>">
    <select name='pro_id'><option value="">[Add new product]<?php
	$sql="SELECT id,product_title FROM product_master ORDER BY product_title";

	$result = mysql_query($sql);
	while($data=mysql_fetch_array($result)){
		if($pro_id == $data["id"]) $set="selected";
		else $set="";
		echo "<option value=" . $data["id"] . " ".$set.">" . $data["product_title"];
	}
	?></select>
    <input type='submit' value='Go'></form>
	</TD>
</TR>
</TABLE>
<H2> Manage Products</H2>
<form name='frm' enctype="multipart/form-data"  action="page_products.php?site_id=<?php echo $_SESSION['SITE_ID']; ?>" method="post">
<input type='hidden' name='ite_id' value="<?php echo $_SESSION['SITE_ID']; ?>">
<input type='hidden' name='pro_id' value="<?php echo $pro_id; ?>">
<link id="skin_css" href="hardcore/webeditor/webeditor.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="hardcore/webeditor/webeditor.js"></script>
<script type="text/javascript">
<!--
// This example web page and its content images are located in the same folder as the web content editor program files so image URLs etc. should not be "shortened"
webeditor.shortenLocalURLsSearch = false;
webeditor.shortenLocalURLsPath = false;
webeditor.shortenLocalURLsBase = false;
// -->

<!--
HardCoreWebEditorToolbar();
// -->
</script>
<TABLE width="100%">
<tr><td>Product title </td><td height="25">
<script type="text/javascript">
<!--
product_title = '<?php echo str_replace("SCRIPT", "SCR'+'IPT", str_replace("script", "scr'+'ipt", str_replace("\t", "\\t", str_replace("\n", "\\n", str_replace("\r", "\\r", str_replace("'", "\'", str_replace("</", "<\/", str_replace("\\", "\\\\", $product_title)))))))); ?>';
content_editor = new HardCoreWebEditor('hardcore/webeditor/', '', 'product_title', product_title, '', 'hardcore/webeditor/hardcore1.css', true, 'manager', '', '', '', '', '', '', '', 'html', '');
// -->
</script>
</td>
</tr>
<tr><td>Product category</td><td>
<select name=product_cat_id onchange="javascript:document.frm.product_category.value=document.frm.product_cat_id[document.frm.product_cat_id.selectedIndex].value;"><?php
	
	$sql="SELECT DISTINCT(product_category) as product_category FROM product_master WHERE product_category!='' ORDER BY product_category";

	$result = mysql_query($sql);
	while($row=mysql_fetch_array($result)){
		$product_cat=$row["product_category"];
		if($product_cat == $product_category) $set="selected";
		else $set="";
		echo "<option value='$product_cat' $set>$product_cat";
	}
?></select>&nbsp;<input name=product_category value="<?php echo $product_category; ?>" size=25 maxlength=30></td></tr>
<tr><td>Product description<BR><FONT SIZE=1>(You can enter<BR>content in<BR>HTML format)</FONT></td><td>
<script type="text/javascript">
<!--
product_desc = '<?php echo str_replace("SCRIPT", "SCR'+'IPT", str_replace("script", "scr'+'ipt", str_replace("\t", "\\t", str_replace("\n", "\\n", str_replace("\r", "\\r", str_replace("'", "\'", str_replace("</", "<\/", str_replace("\\", "\\\\", $product_desc)))))))); ?>';
content_editor = new HardCoreWebEditor('hardcore/webeditor/', '', 'product_desc', product_desc, '', 'hardcore/webeditor/hardcore1.css', true, 'manager', '', '', '', '', '', '', '', 'html', '');
// -->
</script>
<script type="text/javascript">
<!--
HardCoreWebEditorDOMInspector();
// -->
</script>
<TR><td>Product Image </td><td><input  type=file name=product_img value="" size=20>&nbsp; 
	<?php if("$pro_id" !="" && "$product_img" !=""){?>
		<a href="photos/products/<?php echo $pro_id . "_" . $product_img; ?>" target="_n1">View current image</a>&nbsp;&nbsp;<a href='page_products.php?site_id=<?php echo $_SESSION['SITE_ID']; ?>&pro_id=<?php echo $pro_id; ?>&rem_img=y'>Remove image</a><?php } 	 
	elseif("$tmp_id"!=""){?>
		<a href="photos/Temp_products/<?php echo $tmp_id . "_" . $product_img; ?>" target="_n1">View Product image</a><?php } else echo "(No existing image)";
		?> 
	</td></tr>
<tr><td>Product Image alt </td><td><input name=product_img_alt value="<?php echo $product_img_alt; ?>" size=40></td></tr>
<tr><td>Product price </td><td><input name=product_price value="<?php echo $product_price; ?>" size=40 maxlength=8></td></tr>
<tr>
  <td>Size</TD>
  <td><input name="size" value="<?php echo $size; ?>" size="40" maxlength="8" /></TD>
</TR>
<tr>
  <td>Color</TD>
  <td><input name="color" value="<?php echo $color; ?>" size="40" maxlength="8" /></TD>
</TR>
<tr>
  <td>Lot Size </TD>
  <td><input name="lot_size" value="<?php echo $lot_size; ?>" size="40" maxlength="8" /></TD>
</TR>
<tr>
  <td>Lot Discount </TD>
  <td><input name="lot_discount" value="<?php echo $lot_discount; ?>" size="40" maxlength="8" /></TD>
</TR>
<tr>
  <td>Maximum Discount </TD>
  <td><input name="max_discount" value="<?php echo $max_discount; ?>" size="40" maxlength="8" /></TD>
</TR>
<tr><td colspan=2 align=center>
<input type="hidden" name="tmp_pic" value="<?php if($tmp_id!="") echo $tmp_id;?>" />
<input type="hidden" name="tmp_img" value="<?php if($tmp_id!="") echo $tmp_id . "_" .$product_img;?>" />
	<?php
		if("$pro_id" =="") echo "<input type=submit name=action value=Add>";
		else echo "<input type=submit name=action value=Save>&nbsp;<input type=submit name=action value=Delete>";
	?></TD>
</TR>
</TABLE>
</form>
</CENTER>
<?php
	include_once("king_foot.php");
?>